package com.huiquan.icd.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.huiquan.analysis.domain.User;
import com.huiquan.framework.base.EnhancedBaseDao;
import com.huiquan.icd.domain.IcdCode;
import com.huiquan.icd.domain.IcdCodeDto;
import com.ibatis.sqlmap.client.SqlMapClient;

@Repository
public class IcdCodeDao extends EnhancedBaseDao<IcdCode> {

	@Resource(name = "sqlMapClient")
	private SqlMapClient sqlMapClient;

	public static final String space = "ICD_CODE.";

	public IcdCodeDao() {
		super(space);
	}

	@SuppressWarnings("unchecked")
	public List<IcdCodeDto> retrieveDtoList(Map<String, Object> param) {
		return (List<IcdCodeDto>) this.list(space + "RETRIEVE_DTO_LIST", param, sqlMapClient);
	}

	public IcdCodeDto retrieveDtoById(long id) {
		Map<String, Object> param = new HashMap<>();
		param.put("id", id);
		return (IcdCodeDto) this.object(space + "RETRIEVE_DTO_BY_ID", param, sqlMapClient);
	}

	public IcdCodeDto retrieveDtoByCode(String code) {
		Map<String, Object> param = new HashMap<>();
		param.put("code", code);
		return (IcdCodeDto) this.object(space + "RETRIEVE_DTO_BY_CODE", param, sqlMapClient);
	}

	@SuppressWarnings("unchecked")
	public List<IcdCodeDto> retrieveCheckedList() {
		return (List<IcdCodeDto>) this.list(space + "RETRIEVE_CHECKED_LIST", sqlMapClient);
	}

	public void changeDiseaseId(Long oldId, Long newId) {
		Map<String, Object> param = new HashMap<>();
		param.put("oldId", oldId);
		param.put("newId", newId);
		update(space + "CHANGE_DISEASE_ID", param, sqlMapClient);
	}

	public int retrieveExistedSizeByCode(String code) {
		Integer size = (Integer) this.object(space + "RETRIEVE_EXISTED_SIZE_BY_CODE", code, sqlMapClient);
		return size == null ? 0 : size;
	}

	public void updatePid(IcdCode icdCode) {
		update(space + "UPDATE_PID", icdCode, sqlMapClient);
	}

	/**
	 * 
	 * @param newId
	 * @param code
	 * @param type
	 *            1-类目；2-亚目
	 */
	public void batchUpdatePid(Long id, String code, int type) {
		Map<String, Object> param = new HashMap<>();
		param.put("code", code);
		if (type == 1) {
			param.put("gpId", id);
		} else {
			param.put("pId", id);
		}
		update(space + "BATCH_UPDATE_PID", param, sqlMapClient);
	}

	public void deleteImportTemp() {
		delete(space + "DELETE_IMPORT_TEMP", sqlMapClient);
	}

	public void insertImportTemp(List<Map<String, Object>> list) {
		insert(space + "INSERT_IMPORT_TEMP", list, sqlMapClient);
	}

	@SuppressWarnings("unchecked")
	public List<String> getRepeatingCodeInCodeImportTemp() {
		return (List<String>) this.list(space + "GET_REPEATING_CODE_IN_CODE_IMPORT_TEMP", sqlMapClient);
	}

	@SuppressWarnings("unchecked")
	public List<String> getImportUpdateNameError() {
		return (List<String>) this.list(space + "GET_IMPORT_UPDATE_NAME_ERROR", sqlMapClient);
	}

	@SuppressWarnings("unchecked")
	public List<String> getImportDeleteCode() {
		return (List<String>) this.list(space + "GET_IMPORT_DELETE_CODE", sqlMapClient);
	}

	@SuppressWarnings("unchecked")
	public List<String> getCategoryNullError() {
		return (List<String>) this.list(space + "GET_CATEGORY_NULL_ERROR", sqlMapClient);
	}

	@SuppressWarnings("unchecked")
	public List<String> getImportNameAndCategoryMatchingError() {
		return (List<String>) this.list(space + "GET_IMPORT_NAME_AND_CATEGORY_MATCHING_ERROR", sqlMapClient);
	}

	public void persistImportData(long userId, String userName) {
		long startTime = System.currentTimeMillis();
		Map<String, Object> param = new HashMap<>();
		param.put("userId", userId);
		param.put("userName", userName);
		this.update(space + "PERSIST_IMPORT_DATA", param, sqlMapClient);
		logger.info("【Notice】 excute persist import data cost [{}]ms.", System.currentTimeMillis() - startTime);
	}

	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> retrieveAllValidList() {
		return (List<Map<String, Object>>) this.list(space + "RETRIEVE_ALL_VALID_LIST", sqlMapClient);
	}

	public long getCodeUpdateRecordIdBeforeDays(int day) {
		Map<String, Object> param = new HashMap<>();
		param.put("day", day);
		return (Long) this.object(space + "GET_CODE_UPDATE_RECORD_ID", param, sqlMapClient);
	}

	public void addCompletedActionRecord(int type, User user) {
		addActionRecord(type, 1, user);
	}

	public void addUncompletedActionRecord(int type, User user) {
		addActionRecord(type, 0, user);
	}

	public void addActionRecord(int type, int status, User user) {
		Map<String, Object> param = new HashMap<>();
		param.put("type", type);
		param.put("status", status);
		param.put("userId", user.getUserId());
		param.put("userName", user.getRealName());

		insert(space + "INSERT_ACTION_RECORD", param, sqlMapClient);
	}

	public void updateActionRecord(long codeUpdateRecordId, User user) {
		Map<String, Object> param = new HashMap<>();
		param.put("recordId", codeUpdateRecordId);
		param.put("userId", user.getUserId());
		param.put("userName", user.getRealName());
		update(space + "UPDATE_ACTIOIN_RECORD", param, sqlMapClient);
	}

	@SuppressWarnings("unchecked")
	public List<String> getNameAndUrgentFlagMatchingError() {
		return (List<String>) this.list(space + "GET_IMPORT_NAME_AND_URGENT_FLAG_MATCHING_ERROR", sqlMapClient);
	}
	
	public int checkActionRecord() {
		Integer size = (Integer) this.object(space + "RETRIEVE_ACTION_RECORD_BY_NOW",  sqlMapClient);
		return size == null ? 0 : size;
	}
}
